package com.fgxia.admin.controller.operation;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import com.fgxia.admin.service.IAdminAppIndexService;
import com.fgxia.admin.service.IAdminOrderService;
import com.fgxia.admin.service.IAdminWaybillService;
import com.ruoyi.business.domain.bo.admin.AdminFreightQueryBo;
import com.ruoyi.business.domain.bo.admin.AdminOrderQueryBo;
import com.ruoyi.business.domain.vo.OrderInfoVo;
import com.ruoyi.business.domain.vo.admin.AdminCountOrderVo;
import com.ruoyi.business.domain.vo.admin.AdminFreightInfoVo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.FreightStatus;
import com.ruoyi.common.enums.OrderStatus;
import com.ruoyi.common.enums.SysYesNo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 运营管理首页
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/operation/index")
public class AdminAppIndexController extends BaseController {

    private final IAdminAppIndexService appIndexService;

    private final IAdminOrderService orderService;

    private final IAdminWaybillService waybillService;

    /**
     * 获取各类型订单数量
     */
    // @SaCheckPermission("admin:order:count")
    @GetMapping("/order/count")
    public R<AdminCountOrderVo> countOrder() {
        return R.ok(appIndexService.countOrder());
    }

    /**
     * 获取订单列表
     */
    @SaCheckPermission("order:order:list")
    @GetMapping("/order/list")
    public TableDataInfo<OrderInfoVo> listOrder(AdminOrderQueryBo orderQueryBo, PageQuery pageQuery) {
        return orderService.listOrder(orderQueryBo, pageQuery);
    }

    /**
     * 获取运单列表
     */
    @SaCheckPermission("waybill:waybill:list")
    @GetMapping("/waybill/list")
    public TableDataInfo<AdminFreightInfoVo> listWaybill(AdminFreightQueryBo freightQueryBo, PageQuery pageQuery) {
        return waybillService.listWaybill(freightQueryBo, pageQuery);
    }

    /**
     * 获取新订单列表
     */
    @SaCheckPermission("order:new:list")
    @GetMapping("/order/new")
    public TableDataInfo<OrderInfoVo> listNewOrder(AdminOrderQueryBo orderQueryBo, PageQuery pageQuery) {
        orderQueryBo.setOrderStatusList(CollUtil.newArrayList(OrderStatus.INITIALIZED.getCode()));
        return orderService.listOrder(orderQueryBo, pageQuery);
    }

    /**
     * 获取配送中订单列表
     */
    @SaCheckPermission("order:delivery:list")
    @GetMapping("/order/delivery")
    public TableDataInfo<OrderInfoVo> listDeliveryOrder(AdminOrderQueryBo orderQueryBo, PageQuery pageQuery) {
        orderQueryBo.setOrderStatusList(CollUtil.newArrayList(OrderStatus.WAITING_FOR_PICKUP.getCode(), OrderStatus.DELIVERING.getCode()));
        return orderService.listOrder(orderQueryBo, pageQuery);
    }


    /**
     * 获取配送中运单列表
     */
    @SaCheckPermission("waybill:delivery:list")
    @GetMapping("/waybill/delivery")
    public TableDataInfo<AdminFreightInfoVo> listDeliveryWaybill(AdminFreightQueryBo freightQueryBo, PageQuery pageQuery) {
        freightQueryBo.setFreightStatusList(CollUtil.newArrayList(FreightStatus.WAITING_FOR_PICKUP.getCode(), FreightStatus.DELIVERING.getCode()));
        return waybillService.listWaybill(freightQueryBo, pageQuery);
    }

    /**
     * 获取需要上传回单的运单列表
     */
    @SaCheckPermission("waybill:receipt:list")
    @GetMapping("/waybill/receipt")
    public TableDataInfo<AdminFreightInfoVo> listReceipt(AdminFreightQueryBo freightQueryBo, PageQuery pageQuery) {
        freightQueryBo.setFreightStatusList(CollUtil.newArrayList(FreightStatus.WAITING_FOR_RECEIPT.getCode()));
        return waybillService.listWaybill(freightQueryBo, pageQuery);
    }

    /**
     * 获取需打款的订单列表
     */
    @SaCheckPermission("order:payment:list")
    @GetMapping("/order/payment")
    public TableDataInfo<OrderInfoVo> listPaymentOrder(AdminOrderQueryBo orderQueryBo, PageQuery pageQuery) {
        orderQueryBo.setOrderStatusList(CollUtil.newArrayList(OrderStatus.WAITING_FOR_PAYMENT.getCode()));
        return orderService.listOrder(orderQueryBo, pageQuery);
    }

    /**
     * 获取要打款的运单列表
     */
    @SaCheckPermission("waybill:payment:list")
    @GetMapping("/waybill/payment")
    public TableDataInfo<AdminFreightInfoVo> listPayment(AdminFreightQueryBo freightQueryBo, PageQuery pageQuery) {
        freightQueryBo.setFreightStatusList(CollUtil.newArrayList(FreightStatus.WAITING_FOR_PAYMENT.getCode()));
        return waybillService.listWaybill(freightQueryBo, pageQuery);
    }

    /**
     * 获取紧急状态的运单列表
     */
    @SaCheckPermission("waybill:urgent:list")
    @GetMapping("/waybill/urgent")
    public TableDataInfo<AdminFreightInfoVo> listUrgent(AdminFreightQueryBo freightQueryBo, PageQuery pageQuery) {
        freightQueryBo.setIsUrgency(SysYesNo.Y.name());
        return waybillService.listWaybill(freightQueryBo, pageQuery);
    }

}
